package cn.edu.niit.alienlab.jshttplessonserver.dao;

import cn.edu.niit.alienlab.jshttplessonserver.domain.DirectorInfo;
import cn.edu.niit.alienlab.jshttplessonserver.domain.MovieYear;
import cn.edu.niit.alienlab.jshttplessonserver.domain.MovieInfo;
import cn.edu.niit.alienlab.jshttplessonserver.domain.TypeInfo;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.annotations.Param;

import java.util.List;

@Mapper
public interface Movie_infoDao {

    /**
     * API1:获取影片全部分类，按照影片数量从多到少排序
     * @return
     */
    @Select("SELECT mt.type_id,ti.`name`,COUNT(mi.movie_id) as num\n" +
            "FROM type_info ti \n" +
            "JOIN movie_types mt ON mt.type_id=ti.type_id\n" +
            "JOIN movie_info mi ON mt.movie_id=mi.movie_id\n" +
            " GROUP BY mt.type_id,ti.`name` ORDER BY num  DESC")
    public List<TypeInfo> findTypeOderByMovieCount();

    /**
     * API2:获取影片全部年份,按照年份从近到远排序
     * @return
     */
    @Select("SELECT DISTINCT(movie_info.movie_year) from movie_info ORDER BY movie_year DESC")
    public  List<MovieYear> findYear();


    /**
     * AP3:按影片分类、年份、关键字查询影片列表,需支持分页，并且支持单个条件查询以及全部条件查询
     * @param typename
     * @param movie_year
     * @param name
     * @param page
     * @param size
     * @return
     */
    @Select({
            "<script>" ,
            "SELECT DISTINCT c.* " +
                    "FROM movie_types a,type_info b,movie_info c " +
                    "WHERE a.type_id=b.type_id and a.movie_id=c.movie_id ",

            "<if test='typename != null and typename !=\"\"'>",
            "  and b.name =#{typename}",
            "</if>",

            "<if test='movie_year != null and movie_year !=\"\"'>",
            "  and c.movie_year =#{movie_year}",
            "</if>",

            "<if test='name != null and name !=\"\"'>",
            "  and c.name LIKE '%${name}%'",
            "</if>",

            " limit #{page},#{size}",

            "</script>"
    })
    public List<MovieInfo> findMoviesByConditions(@Param("typename") String typename,
                                                  @Param("movie_year") String movie_year,
                                                  @Param("name") String name,
                                                  @Param("page") Integer page,
                                                  @Param("size") Integer size);


    @Select("SELECT * FROM type_info a,movie_types b WHERE a.`type_id`=b.`type_id` AND b.`movie_id`=#{movie_id}")
    public List<TypeInfo> findTypesByMovie(@Param("movie_id") Integer movie_id);



    /**
     * API4:按影片id查询影片详细信息，包含导演、编剧、出品公司详细信息
     * @param movie_id
     * @return
     */
    @Select("select mi.* from movie_info mi where mi.movie_id=#{movie_id}")
    @Results({
            @Result(
                    property = "directorInfo",
                    javaType = DirectorInfo.class,
                    column = "movie_id",
                    one = @One(select = "cn.edu.niit.alienlab.jshttplessonserver.dao.DirectorInfoDao.selectDirectorByMovieId")
            ),
            @Result(
                    property = "writerInfos",
                    javaType = List.class,
                    column = "movie_id",
                    many = @Many(select = "cn.edu.niit.alienlab.jshttplessonserver.dao.WriterInfoDao.selectWriterInfoByMovieId")

            ),
            @Result(
                    property = "companyInfos",
                    javaType = List.class,
                    column = "movie_id",
                    many = @Many(select = "cn.edu.niit.alienlab.jshttplessonserver.dao.CompanyInfoDao.selectCompanyInfoByMovieId")

            ),
            @Result(
                    property = "typeInfoList",
                    javaType = List.class,
                    column = "movie_id",
                    many = @Many(select = "cn.edu.niit.alienlab.jshttplessonserver.dao.Movie_infoDao.findTypesByMovie")

            )
    })
    public List<MovieInfo> findMovieByMovieID(@Param("movie_id") Integer movie_id);



}
